package admin.shell.commands;

import game.MatchStatistics;

import java.util.List;

import admin.ui.ProxyMediator;

public class ListMatchsCommand extends ShellCommand {

	@Override
	public void execute(ProxyMediator proxy, String[] parameters) {
			
		List<MatchStatistics> matchs = proxy.listMatchs();
		
		if (matchs.size() == 0) {
			System.out.println("No matchs running");
			return;
		}
			
		printMatchList(matchs);
	}
	
	private void printMatchList(List<MatchStatistics> matchs) {
		
		System.out.printf("%10s  %-20s %-30s %s\n", "ID", "Game", "Date started", "Players");
		
		for (MatchStatistics match : matchs) {	
			String playersStr = "";
			for (String playerName : match.getPlayers()) {
				if (match.getWinner() != null && match.getWinner().equals(playerName)) {
					playersStr += "[" + playerName + "], ";
				} else {
					playersStr += playerName + ", ";
				}
			}
			playersStr = playersStr.substring(0, playersStr.length() - 2);
			System.out.printf("%10d  %-20s %-30s %s\n", match.getMatchId(), match.getGameName(), match.getDateStarted() != null ? match.getDateStarted() : "n/a", playersStr);
		}
		
		System.out.println("\nTotal: " + matchs.size());
	}

}
